﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using EthernetTools.Utils;

namespace EthernetTools.DataBindinds
{
    public class ConrolBinding
    {
        protected Control stringContainer;

        protected Control additionalStringDisplayer;

        protected bool isCorrect = false;

        private bool isBinded = false;

        /// <summary>
        /// The control which was binded to
        /// </summary>
        public Control StringContainer
        {
            get
            {
                return stringContainer;
            }
        }

        /// <summary>
        /// Gets the Raw byte[] data of binding (if present), null otherwise
        /// </summary>
        public virtual byte[] RawData
        {
            get
            {
                return null;
            }
            set
            {

            }
        }

        /// <summary>
        /// Was data in control successfully parsed
        /// </summary>
        public virtual bool IsCorrect
        {
            get
            {
                return isCorrect && isBinded;
            }
            protected set
            {
                if (!isBinded)
                    return;

                isCorrect = value;
                if (stringContainer != null)
                {
                    if (isCorrect)
                    {
                        stringContainer.BackColor = Color.LightGreen;

                        if (additionalStringDisplayer != null)
                            additionalStringDisplayer.Text = ByteUtils.ByteArrToString(RawData);
                    }
                    else
                    {
                        stringContainer.BackColor = Color.Pink;

                        if (additionalStringDisplayer != null)
                            additionalStringDisplayer.Text = "";
                    }
                }
            }
        }

        public virtual void Bind()
        {
            isBinded = true;
        }

        public virtual void UnBind()
        {
            isBinded = false;
        }
    }
}
